package com.tunan.utils;

import java.awt.Graphics2D;
import java.awt.Image;

import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.HashMap;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.filechooser.FileSystemView;

import sun.awt.shell.ShellFolder;

public class FileImages {
	private static HashMap<String,byte[]> EXTIMAGES=new HashMap<String, byte[]>();
	private static HashMap<String,byte[]> EXTBIGIMAGES=new HashMap<String, byte[]>();
	private static FileSystemView fsv=FileSystemView.getFileSystemView();
	public static synchronized byte[]  get(String ext){
		if(EXTIMAGES.get(ext)==null){
			File tempFile=null;
			ByteArrayOutputStream out =null;
			byte[] bs=new byte[0];
			try {
				tempFile=File.createTempFile("icon", "."+ext);
				ImageIcon smallIcon =(ImageIcon) fsv.getSystemIcon(tempFile);
				int height = smallIcon.getIconHeight();   
				BufferedImage image = new BufferedImage(smallIcon.getIconWidth(), height,   
				        BufferedImage.TYPE_INT_RGB);   
				Graphics2D g = image.createGraphics();   
				image = g.getDeviceConfiguration().createCompatibleImage(smallIcon.getIconWidth(), height,   
				        Transparency.TRANSLUCENT);   
				g.dispose();   
				g = image.createGraphics();   
				g.drawImage(smallIcon.getImage(), 0, 0, smallIcon.getIconWidth(), height, null);   
				g.dispose();   
				out= new ByteArrayOutputStream();      
				ImageIO.write(image, "png", out);
				bs=out.toByteArray();
				EXTIMAGES.put(ext, bs);
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				if(tempFile!=null){
					tempFile.delete();
				}
				if(out!=null){
					try {
						out.close();
					} catch (Exception e2) {
					}
				}
			}
			return bs;
		}else{
			return EXTIMAGES.get(ext);
		}

	}
	public static synchronized byte[]  getBig(String ext){
		if(EXTBIGIMAGES.get(ext)==null){
			File tempFile=null;
			ByteArrayOutputStream out =null;
			byte[] bs=new byte[0];
			try {
				tempFile=File.createTempFile("icon", "."+ext);
				ShellFolder sf=ShellFolder.getShellFolder(tempFile);
				Image bigImage =(Image) sf.getIcon(true);
				int height = bigImage.getHeight(null);   
				BufferedImage image = new BufferedImage(bigImage.getWidth(null), height,   
						BufferedImage.TYPE_INT_RGB);   
				Graphics2D g = image.createGraphics();   
				image = g.getDeviceConfiguration().createCompatibleImage(bigImage.getWidth(null), height,   
						Transparency.TRANSLUCENT);   
				g.dispose();   
				g = image.createGraphics();   
				g.drawImage(bigImage, 0, 0, bigImage.getWidth(null), height, null);   
				g.dispose();   
				out= new ByteArrayOutputStream();      
				ImageIO.write(image, "png", out);
				bs=out.toByteArray();
				EXTBIGIMAGES.put(ext, bs);
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				if(tempFile!=null){
					tempFile.delete();
				}
				if(out!=null){
					try {
						out.close();
					} catch (Exception e2) {
					}
				}
			}
			return bs;
		}else{
			return EXTBIGIMAGES.get(ext);
		}
		
	}
}
